/*
 * Copyright 2008 Federal Emergency Management Agency
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0 
 *    
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 *  
 */
package gov.fema.hazus.bdc.client.ui;

import gov.fema.hazus.bdc.client.Constants;
import gov.fema.hazus.bdc.client.UIController;
import gov.fema.hazus.bdc.client.common.entities.AbstractClickListener;
import gov.fema.hazus.bdc.client.controller.Controller;
import gov.fema.hazus.bdc.shared.entity.AEBMResults;
import gov.fema.hazus.bdc.shared.entity.Address;
import gov.fema.hazus.bdc.shared.entity.Bracing;
import gov.fema.hazus.bdc.shared.entity.Building;
import gov.fema.hazus.bdc.shared.entity.Capacity;
import gov.fema.hazus.bdc.shared.entity.Contact;
import gov.fema.hazus.bdc.shared.entity.EarthquakeBuildingDesign;
import gov.fema.hazus.bdc.shared.entity.EarthquakeHazard;
import gov.fema.hazus.bdc.shared.entity.EarthquakeSecondaryCharacteristics;
import gov.fema.hazus.bdc.shared.entity.Economy;
import gov.fema.hazus.bdc.shared.entity.FloodBasement;
import gov.fema.hazus.bdc.shared.entity.FloodBuildingDesign;
import gov.fema.hazus.bdc.shared.entity.FloodHazard;
import gov.fema.hazus.bdc.shared.entity.Location;
import gov.fema.hazus.bdc.shared.entity.ManufacturedHousing;
import gov.fema.hazus.bdc.shared.entity.Roof;
import gov.fema.hazus.bdc.shared.entity.WallsAndOpenings;
import gov.fema.hazus.bdc.shared.entity.WindBuildingDesign;
import gov.fema.hazus.bdc.shared.entity.WindHazard;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/*
 * The Left Navigation allows the user to navigate between
 * different views of the inventory (list view, building
 * detail, and map view). Links in the LeftNavigation
 * should trigger methods in the UIController, which 
 * will delegate to the MainUI or other sub-user interfaces
 */
public class LeftNavigation extends Composite {
	
    private static LeftNavigation singleton;
    public static LeftNavigation get()
    {
        if( singleton == null )
        {
            singleton = new LeftNavigation();
        }
        return singleton;
    }
    
	private VerticalPanel main;
	private Constants constants;
	public HashMap listeners;
	private Hyperlink addBuilding;
	private Hyperlink listBuildings;
    private Hyperlink mapBuildings;    
	private ArrayList links;
   
	
	public LeftNavigation()
	{
		
        singleton = this;
        
        // constants
		constants = (Constants)GWT.create(Constants.class);
		
		// set up array of links
		links = new ArrayList();
		
		// setup 
		main = new VerticalPanel();
		main.setStyleName("leftnav");
		
		// left nav items
		
		addBuilding = new Hyperlink();
		addBuilding.setText(constants.leftnavAddBuilding());
		addBuilding.setStyleName("leftnav-item");
		addBuilding.addClickListener(new AddBuildingClickListener());
		main.add(addBuilding);
		links.add(addBuilding);
		
		listBuildings = new Hyperlink();
		listBuildings.setText(constants.leftnavListBuildings());
		listBuildings.setStyleName("leftnav-item");
		listBuildings.addClickListener(new BuildingListClickListener());
		main.add(listBuildings);
		links.add(listBuildings);
        
        mapBuildings = new Hyperlink();
        mapBuildings.setText(constants.leftnavMapBuildings());
        mapBuildings.setStyleName("leftnav-item");
        mapBuildings.addClickListener(new BuildingMapClickListener());
        main.add(mapBuildings);
        links.add(mapBuildings);
		
		main.setWidth("150");
		
		initWidget(main);
		
	}
	
	/*
	 * The ClickListener to add a new building
	 */
	protected class AddBuildingClickListener extends AbstractClickListener
	{

		public void handleClick(Widget sender) {
			
			GWT.log("handling AddBuildingClickListener", null);
            highlightLeftNav(constants.leftnavAddBuilding());
            
            Building building = new Building();
            /*
             * TODO: create and set all the child components
             */
            building.setAddress(new Address());
            building.setAebmResults(new AEBMResults());
            building.setBracing(new Bracing());
            building.setCapacity(new Capacity());
            building.setContact(new Contact());
            building.setEarthquakeBuildingDesign(new EarthquakeBuildingDesign());
            building.setEarthquakeHazard(new EarthquakeHazard());
            building.setEarthquakeSecondaryCharacteristics(new EarthquakeSecondaryCharacteristics());
            building.setEconomy(new Economy());
            building.setFloodBasement(new FloodBasement());
            building.setFloodBuildingDesign(new FloodBuildingDesign());
            building.setFloodHazard(new FloodHazard());
            building.setLocation(new Location());
            building.setManufacturedHousing(new ManufacturedHousing());
            building.setRoof(new Roof());
            building.setWallsAndOpenings(new WallsAndOpenings());
            building.setWindBuildingDesign(new WindBuildingDesign());
            building.setWindHazard(new WindHazard());
            
            
            BuildingEditor.get().updateBuilding(building);
            Controller.get().mainShowBuildingEditor();
		}
		
	}	
	
	protected class BuildingListClickListener extends AbstractClickListener
	{
		public void handleClick(Widget sender) {
			BuildingList.get().resetAndGetBuildings();
		}
	}
    
    protected class BuildingMapClickListener extends AbstractClickListener
    {
        public void handleClick(Widget sender) {
            GWT.log("handling BuildingMapClickListener", null);
            Controller.get().mainShowBuildingMap();
            UIController.get().actionGetBuildings();
            /*
        if( this.buildings == null )
        {
            GWT.log("fetching buildings from proxy", null);
            SearchServiceProxy.get().getBuildings(buildingMapHandler);
        }
        else
        {
            GWT.log("already have buildings from proxy, just show map", null);
            showBuildingMap();
        }	
             */
        }
    }    
	
	public void highlightLeftNav(String leftNavText)
	{
		for (Iterator i = links.iterator(); i.hasNext();) {
			Hyperlink link = (Hyperlink) i.next();
			if (link.getText().equals(leftNavText))
			{
				link.setStyleName("leftnav-item-selected");
			}
			else
			{
				link.setStyleName("leftnav-item");
			}
		}
	}
    
    public void removeHighlightLeftNav()
    {
        for (Iterator i = links.iterator(); i.hasNext();) {
            Hyperlink link = (Hyperlink) i.next();
            link.setStyleName("leftnav-item");
        }
    }

    public void showBuildingList() {
        showBuildingListAction();
    }
    
    private void showBuildingListAction()
    {
        //UserList.get().setSearchFlag(false);
        //UserList.get().resetAndGetBuildings();
    	// MainUI.get().showBuildingList();
    	// Instead of MainUI, go to Controller
    	Controller.get().buildingListGetBuildings();
    }

	public void highlightMapLeftNav() {
		highlightLeftNav(constants.leftnavMapBuildings());
	}
	
	public static int propertyGetWidth()
	{
		GWT.log("LEFTNAVIGATION: getoffsetwidth=" + LeftNavigation.get().getOffsetWidth(), null);
		return LeftNavigation.get().getOffsetWidth();
	}

}
